<template>
	<div>
		<!-- 选择人员弹窗 -->
    <el-dialog 
      title="选择人员"
      append-to-body
      :visible.sync="chooseUserDialog"
      :close-on-click-modal="false" 
      :before-close="handleCloseGlcjUser"
      width="60%"
    >

      <!-- 表格内容 -->
      <avue-crud 
        :option="userOption"
        :data="userData.filter((item) => item.realName.includes(chooseUserName))"
        @selection-change="userSelectionChange"
        ref="userCrud"
      >
        <template slot="menuLeft">
          <el-input
            placeholder="请输入人员名称"
            v-model="chooseUserName"
            size="small">
          </el-input>
        </template>
      </avue-crud>

      <!-- 按钮 -->
      <div class="choose_user_footer">
        <el-button 
          type="primary"
          size="small"
          @click="chooseUser()">
          确 认
        </el-button>
        <el-button 
          size="small"
          @click="cancelChooseUser()">
          取 消
        </el-button>
      </div>
    </el-dialog>
	</div>
</template>

<script>
import { getUserList } from "@/api/saferisk/riskpoint";

export default {
  props: {
    chooseUserDialog: {default: false},
    chooseUserName: {default: ""},
    selectUserTags: {default: []}
  },
  data() {
    return {
      userData: [],
      userSelectionList: [],
      userOption: {
        calcHeight: 30,
        tip: false,
        index: true,
        selection: true,
        addBtn: false,
        refreshBtn: false,
        columnBtn: false,
        menu: false,
        column: [
          {
            label: "人员名称",
            prop: "realName",
            align: "center"
          },
          {
            label: "性别",
            prop: "sexName",
            align: "center"
          },
          {
            label: "电话",
            prop: "phone",
            align: "center"
          }
        ]
      }
    }
  },
  watch: {
    chooseUserDialog(){
      if(this.chooseUserDialog === true){
        this.initData();
      }
    }
  },
  mounted() {
    this.initData();
  },
  methods: {

    // 初始化评估历史弹窗
    initData() {
      this.getUsers();
    },

    // 获取人员列表
    getUsers() {
      getUserList().then(res => {
        this.userData = res.data.data;
      }).then(() => {
        this.checkSelectedUsers();
      });
    },

    // 勾选已在列表中的人员
    checkSelectedUsers() {
      if(this.selectUserTags.length !== 0) {
        let userIds = [];
        this.selectUserTags.forEach(item => {
          userIds.push(item.id);
        });
        this.$nextTick(() => {
          this.userData.forEach(row => {
            if(userIds.indexOf(row.id) >= 0 && this.$refs.userCrud !== undefined) {
              this.$refs.userCrud.toggleRowSelection(row, true);
            }
          })
        });
      } else {
        if(this.$refs.userCrud !== undefined) {
          this.$refs.userCrud.toggleSelection();
        }
      }
    },

    // 勾选人员
    userSelectionChange(list) {
      this.userSelectionList = list;
    },

    // 确认勾选人员
    chooseUser() {
      if(this.userSelectionList.length === 0) {
        this.$message.warning("请选择人员");
        return;
      }
      this.$emit('chooseUser', this.userSelectionList);
    },

    // 关闭选择人员弹窗
    cancelChooseUser() {
      this.$emit('cancelChooseUser');
    },

    // 关闭选择人员弹窗
    handleCloseGlcjUser(done) {
      done();
      this.$emit('handleCloseGlcjUser');
    },

  }
}
</script>